import random

import var
from control.new_control import new_process
from gui.ui_about import CreateAboutWindow
from gui.ui_setting import SettingParams, CreateSettingWindow
from utils.delLayout import delLayout

from var import core, screen, author
from workThread.FCFSWorker import FCFSWorker
from workThread.SJFWorker import SJFWorker


def new_click(ui_main):
    core.reset()
    for i in range(var.rand_num):
        arrival_time = random.randint(0, 10)
        burst_time = random.randint(1, 5)
        core.addJCB(arrival_time, burst_time)
    ui_main.refreshInit(core.getJCBList())


def about_click():
    print(author)
    aboutWin = CreateAboutWindow(screen)
    aboutWin.show()
    aboutWin.exec()


def setting_click(ui_main):
    new_params = SettingParams(new_process)
    newWin = CreateSettingWindow(screen, new_params, ui_main)
    newWin.show()
    newWin.exec()


def reset_click(ui_main):
    core.reset()
    ui_main.time_show.setText("时间：0")
    ui_main.refreshInit(core.getJCBList())
    delLayout(ui_main.output1)


def run_fcfs_click(ui_main):
    print("receive run_click")
    global ui_worker
    ui_worker = FCFSWorker(core.getJCBList())
    ui_worker.current_time_signal.connect(ui_main.refreshTime)
    ui_worker.ready_signal.connect(ui_main.resetWaitList)
    ui_worker.run_signal.connect(ui_main.resetRunList)
    ui_worker.finish_signal.connect(ui_main.resetFinishList)

    ui_worker.output1_signal.connect(ui_main.flushOutput1)
    ui_worker.start()


def run_sjf_click(ui_main):
    print("receive run_click2")
    global ui_worker_sjf
    ui_worker_sjf = SJFWorker(core.getJCBList())
    ui_worker_sjf.current_time_signal.connect(ui_main.refreshTime)
    ui_worker_sjf.ready_signal.connect(ui_main.resetWaitList)
    ui_worker_sjf.run_signal.connect(ui_main.resetRunList)
    ui_worker_sjf.finish_signal.connect(ui_main.resetFinishList)

    ui_worker_sjf.output1_signal.connect(ui_main.flushOutput1)
    ui_worker_sjf.start()
